using UnityEngine;
using System.Collections;


public class Hook : MonoBehaviour
{
	public Character				_character;
	public float					_mass = 1.4f;
	public bool						_isHooked = false;
	
	private Vector3 				_lockedAxis;

	// Use this for initialization
	void Start ()
	{
		gameObject.collider.isTrigger = true;
		gameObject.rigidbody.mass = _mass;
		_lockedAxis = new Vector3(1,1,0); //This vector descripts the 2D-Movingplane in the game!
	}
	
	// Update is called once per frame
	void Update ()
	{
		if ( !_isHooked )
		{
			

			//locking axis -> prefent moving on locked axis...
			Vector3 _v = gameObject.rigidbody.velocity;
			gameObject.rigidbody.velocity = new Vector3( _v.x * _lockedAxis.x,
												_v.y * _lockedAxis.y,
												_v.z * _lockedAxis.z );

			gameObject.rigidbody.angularVelocity = new Vector3( 0, 0, 0 );

			
			//-----------DEBUG----------------->
			//Debug.Log( "Update hook!" );
		}
		else
		{
			//gameObject.rigidbody.velocity = new Vector3( 0, 0, 0 );
			
		}
	}
	
	void OnTriggerEnter(Collider other)
	{
		//-----------DEBUG----------------->
		Debug.Log( "Hook-Trigger" );
		Hookable _ha = null;
		_ha = other.gameObject.GetComponent<Hookable>();
		if ( _ha != null && _ha._hookable && !other.gameObject.GetComponent<Planet>() )
		{
			_isHooked = true;
			GetComponent<CustomGravity>()._enabled = false;
			gameObject.transform.parent = other.transform;
			gameObject.rigidbody.isKinematic = true;
			//-----------DEBUG----------------->
			Debug.Log( "Hooked" );
			
		}
	}

	void OnCollisionExit(Collision collisionInfo)
	{

	}
}
